package com.yic.module.base.controller.admin.erpcentermapping;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.dal.dataobject.factory.FactoryDO;
import com.yic.module.base.dal.dataobject.workshop.WorkshopDO;
import com.yic.module.base.service.factory.FactoryService;
import com.yic.module.base.service.workshop.WorkshopService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.base.controller.admin.erpcentermapping.vo.*;
import com.yic.module.base.dal.dataobject.erpcentermapping.ErpCenterMappingDO;
import com.yic.module.base.convert.erpcentermapping.ErpCenterMappingConvert;
import com.yic.module.base.service.erpcentermapping.ErpCenterMappingService;

@Tag(name = "管理后台 - ERP工作中心映射")
@RestController
@RequestMapping("/base/erp-center-mapping")
@Validated
public class ErpCenterMappingController {

    @Resource
    private ErpCenterMappingService erpCenterMappingService;
    @Resource
    private FactoryService factoryService;
    @Resource
    private WorkshopService workshopService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP工作中心映射")
    @PreAuthorize("@ss.hasPermission('base:erp-center-mapping:create')")
    public CommonResult<Long> createErpCenterMapping(@Valid @RequestBody ErpCenterMappingCreateReqVO createReqVO) {
        return success(erpCenterMappingService.createErpCenterMapping(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP工作中心映射")
    @PreAuthorize("@ss.hasPermission('base:erp-center-mapping:update')")
    public CommonResult<Boolean> updateErpCenterMapping(@Valid @RequestBody ErpCenterMappingUpdateReqVO updateReqVO) {
        erpCenterMappingService.updateErpCenterMapping(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP工作中心映射")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:erp-center-mapping:delete')")
    public CommonResult<Boolean> deleteErpCenterMapping(@RequestParam("id") Long id) {
        erpCenterMappingService.deleteErpCenterMapping(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP工作中心映射")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:erp-center-mapping:query')")
    public CommonResult<ErpCenterMappingRespVO> getErpCenterMapping(@RequestParam("id") Long id) {
        ErpCenterMappingDO erpCenterMapping = erpCenterMappingService.getErpCenterMapping(id);
        return success(ErpCenterMappingConvert.INSTANCE.convert(erpCenterMapping));
    }

    @GetMapping("/list")
    @Operation(summary = "获得ERP工作中心映射列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('base:erp-center-mapping:query')")
    public CommonResult<List<ErpCenterMappingRespVO>> getErpCenterMappingList(@RequestParam("ids") Collection<Long> ids) {
        List<ErpCenterMappingDO> list = erpCenterMappingService.getErpCenterMappingList(ids);
        return success(ErpCenterMappingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP工作中心映射分页")
    @PreAuthorize("@ss.hasPermission('base:erp-center-mapping:query')")
    public CommonResult<PageResult<ErpCenterMappingRespVO>> getErpCenterMappingPage(@Valid ErpCenterMappingPageReqVO pageVO) {
        PageResult<ErpCenterMappingDO> pageResult = erpCenterMappingService.getErpCenterMappingPage(pageVO);
        PageResult<ErpCenterMappingRespVO> respVo = ErpCenterMappingConvert.INSTANCE.convertPage(pageResult);
        respVo.getList().forEach(resp -> {
            //封装工厂
            FactoryDO factory = factoryService.getFactory(resp.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                resp.setFactoryName(factory.getName());
            }

            //封装生产车间
            if (ObjectUtil.isNotEmpty(resp.getWorkshopId())) {
                WorkshopDO workshop = workshopService.getWorkshop(resp.getWorkshopId());
                if (ObjectUtil.isNotEmpty(workshop)) {
                    resp.setWorkshopName(workshop.getName());
                }
            }
        });
        return success(respVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP工作中心映射 Excel")
    @PreAuthorize("@ss.hasPermission('base:erp-center-mapping:export')")
    @OperateLog(type = EXPORT)
    public void exportErpCenterMappingExcel(@Valid ErpCenterMappingExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ErpCenterMappingDO> list = erpCenterMappingService.getErpCenterMappingList(exportReqVO);
        // 导出 Excel
        List<ErpCenterMappingExcelVO> datas = ErpCenterMappingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "ERP工作中心映射.xls", "数据", ErpCenterMappingExcelVO.class, datas);
    }

}
